﻿##启动主程序,创建工程模板
##
##							  pb -- ddz 改名
##					gamepart--res -- sound -ddz 改名
##							  src -- app -- part -- ddz 改名
##								  -- Default --全考
##								     LuaExtend			
##
##					components--
##		
##
##					config  改前缀
##
##					
##					framework  全考，生成submodule
##
##					proto  --空文件夹
##
## project_name --	
##					gameui
##
##
##
##
import os,sys,shutil,re
flipFiles = ['.git','framework'];
notflipFiles = ['.gitignore']
def changeFileName(filename,orgname,desname):
	searchname = ''
	for c in orgname:
		searchname = searchname + r'[' +c.lower()+ r'|' +c.upper() + r']'
	#print(searchname);
	tempname = None ;
	currentname = re.search(searchname, filename);
	#循环递归查找
	#print("filename->",filename);
	while currentname:
		localname = currentname.group(0) #取得需要替换的字符串
		if localname.isupper():
			desname = desname.upper();
		elif localname[0].isupper():
			desname = desname.capitalize(); 
		#print("desname->",desname);
		if not tempname:
			tempname = filename
		tempname = tempname.replace(localname,desname)
		#print("filename->",filename);
		#print(tempname);
		currentname = re.search(searchname, tempname);
		#if currentname:
			#print('currentname->',currentname.group(0))
			#print("loop")
	return tempname;


def readFileAndChange(filename,orgname,desname):
	lines = ''
	with open(filename, 'r+') as f:
		lines = f.readlines()
	count = len(lines)
	#print('count',count)
	#print('lines->',lines)
	while count>0:
		str = lines[count-1]
		cname = changeFileName(str,orgname,desname)
		if cname:
			lines[count-1] = cname
		count = count - 1
	#print('lines->',lines)
	with open(filename, 'w') as f:
		f.writelines(lines)


def isExistFiles(path):
	if path in notflipFiles:
		return False ;
	for file in flipFiles:
		str = r'^'+file+r'.*$';
		if re.search(str, path):
			return True;
	return False;

addressList = ['.png','.jpg','.plist','.wav','.mp3'];


def copyProject(path,desPath,orgname,desname):
	for x in os.listdir(path):
		tpath = os.path.join(path,x)
		print("search file->",tpath)
		if isExistFiles(x):
			print("not copy")
		elif os.path.isdir(tpath):		
			tempName = changeFileName(x,orgname,desname);
			dirpath = ''
			if tempName:
				dirpath = os.path.join(desPath,tempName)
			else:
				dirpath = os.path.join(desPath,x)
			if not os.path.exists(dirpath):
				os.makedirs(dirpath)
			copyProject(tpath,dirpath,orgname,desname);
			print("2");	
		else:
			#print("filePath->",tpath)
			tempName = changeFileName(x,orgname,desname);
			desFilePath = '';
			if tempName:
				#print("tempName->",tempName)
				#print("tpath->",tpath)
				#print("despath->",os.path.join(desPath,tempName))
				desFilePath = os.path.join(desPath,tempName);
			else:
				desFilePath = os.path.join(desPath,x)
			#print("desFilePath->",desFilePath);
			shutil.copyfile(tpath,desFilePath);
			#修改文件内的数据
			#print(os.path.splitext(desFilePath)[1] )
			if os.path.splitext(desFilePath)[1] not in addressList:
				readFileAndChange(desFilePath,orgname,desname)

#git 存储的地址
gitpath = [];
giturl = [];


def initgitBash(despath):
	os.chdir(despath);
	os.system(' git init');
	os.system(' git remote add origin ' + "https://gitee.com/TanLvXu258/testGit.git");
	os.system(' git pull origin master ');



def initsubgit(path,despath,orgname,desname):
	#获取submodules地址
	submodules = os.path.join(path,".gitmodules");
	lines = '';
	with open(submodules, 'r+') as f:
		lines = f.readlines();
	count = len(lines)
	print(lines)
	while count>0:
		str = lines[count-1]
		cname = changeFileName(str,orgname,desname)
		if cname:
			lines[count-1] = cname
		#print(lines[count-1])
		if re.search('=',lines[count-1]):
			#print("13")
			a,b = lines[count-1].split('=');
			if a.strip() == 'url':
				giturl.append(b.strip())
			elif a.strip() == 'path':
				gitpath.append(b.strip())	
		count = count - 1
	if len(giturl) > 0:
		for i in range(0,len(giturl)):
			os.system(' git submodule add '+ giturl[i]+' ' + gitpath[i]);


def copyProjectFirst(path,desPath,orgname,desname):
	rootgitpath = os.path.join(path,".git");
	if not os.path.exists(rootgitpath):
		return 
	#initgitBash(desPath);
	if os.path.exists(rootgitpath):
		copyProject(path,desPath,orgname,desname);#拷贝工程
	#initsubgit(path,desPath,orgname,desname);
	



#copyProjectFirst("D:/projectNew/ddz_client_new","D:/projectNew/test22",'ddz','gdy');
#print(changeFileName('ddzstring_zh','ddz','gdy'));
#readFileAndChange('D:/projectNew/templateModel/gamepart/src/app/part/gdy/GdyPartConfig.lua','ddz','gdy');
# def file_extension(path):
# 	return os.path.splitext(path)[1] 			
# print(file_extension(os.path.join('D:/projectNew/templateModel/gamepart/src/app/part/gdy','GdyPartConfig.lua')));	